package com.uzai.console.web.fd;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.dto.fadan.FollowMsgDto;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.Merchant;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.fd.FdFollowSendSyncService;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发单自动跟发发送
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "自动跟发发单发送",tags = "个人微信|发单|自动跟发发单发送")
@ApiSort(105)
@RestController
public class FdFollowSendController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(FdFollowSendController.class);

    @Autowired
    private FdFollowSendSyncService fdFollowSendSyncService;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "跟发发单发送内容", notes = "跟发发单发送内容")
    @ApiOperationSupport(order = 1)
    @PostMapping("/fd/followSend")
    public Object followSend(@RequestBody FollowMsgDto followMsgDto){

        logger.info("--发单跟发发送内容--fdInfoDto={}", JSONObject.toJSONString(followMsgDto));

        if(followMsgDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商id不能为空");
        }

        Merchant merchant = merchantMapper.selectById(followMsgDto.getMerId());
        if(merchant == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        if(followMsgDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人唯一id不能为空");
        }

        if(followMsgDto.getDeviceType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人类型不能为空");
        }

        DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(followMsgDto.getDeviceType());
        if(deviceTypeEnum == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器类型不存在");
        }

        Device device = deviceService.findDeviceInfoByDeviceUniqueId(followMsgDto.getDeviceUniqueId(), deviceTypeEnum.getKey(), followMsgDto.getMerId());
        if(device == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控机器人不存在");
        }

        if(followMsgDto.getMsgId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "消息id不能为空");
        }

        if(StringUtils.isBlank(followMsgDto.getGroupWxid())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控群wxid不能为空");
        }

        if(StringUtils.isBlank(followMsgDto.getSendWxid()) ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被监控群成员wxid不能为空");
        }

        //2024-01-04修改，将及时处理跟发消息先保存到数据库，然后在执行跟发业务
        //fdFollowSendSyncService.followSendSync(followMsgDto);
        fdFollowSendSyncService.saveFollowSendMsg(followMsgDto);

        return true;
    }


}
